#include "importprocess.h"
#include "piocore.h"
#include "importmodule.h"
#include <QMessageBox>

ImportProcess::ImportProcess(PioCore * core, ImportModule * module, QList<int> selection, bool deleteSource, bool ejectMedium, QObject *parent)
    : QThread(parent),
      m_core(core),
      m_module(module),
      m_selection(selection),
      m_deleteSource(deleteSource),
      m_ejectMedium(ejectMedium),
      m_stop(false)
{
}

void ImportProcess::stop()
{
    m_stop = true;
}

void ImportProcess::run()
{
    QString path = QString("%1/import-%2").arg(m_core->libraryPath()).arg(QDate::currentDate().toString("yyyy-MM-dd"));
    int failures = 0;
    for(int i = 0; i < m_selection.size(); i++) {
        emit progress(i);
        if(m_stop)
            return;
        if(m_module->import(m_selection[i], path)) {
            if(m_deleteSource)
                m_module->remove(m_selection[i]);
        }
        else
            failures++;
    }
    if(m_ejectMedium && !m_module->eject())
        QMessageBox::warning(0, tr("eject media"), tr("Could not eject medium."));
}
